package solicitudes.dao.impl;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import solicitudes.dao.DAORol;
import solicitudes.entity.Rol;

/***
 * 
 * @author MARIA ALEJANDRA CELEMIN R.
 * @version 1.0
 * 
 * Clase que implementa los metodos de la interfaz DAORol
 * @see DAORol
 *
 */
public class DAORolImpl extends HibernateDaoSupport implements DAORol {

	private static Logger logger = Logger.getLogger(DAORolImpl.class);
	
	/* (non-Javadoc)
	 * @see solicitudes.dao.DAORol#obtenerRoles()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Rol> obtenerRoles() {
		
		Session session = getSession(); 
		List<Rol> roles = new ArrayList<Rol>();
		
		try {
			roles = session.createQuery("from Rol").list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return roles;
	}

	/* (non-Javadoc)
	 * @see solicitudes.dao.DAORol#obtenerRol()
	 */
	@Override
	public Rol obtenerRol(Integer codRol) {
	
		Session session = getSession();
		Rol rol = null;
		
		try {
			rol = (Rol) session.get(Rol.class, codRol);
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
		return rol;
	}

	/* (non-Javadoc)
	 * @see solicitudes.dao.DAORol#guardarRol(solicitudes.entity.Rol)
	 */
	@Override
	public void guardarRol(Rol rol) {
		
		Session session = getSession(); 
		session.beginTransaction();
		
		try {
			session.save(rol);
			session.getTransaction().commit();
			
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}		
	}

	/* (non-Javadoc)
	 * @see solicitudes.dao.DAORol#eliminarRol(solicitudes.entity.Rol)
	 */
	@Override
	public void eliminarRol(Rol rol) {
		
		Session session = getSession(); 
		session.beginTransaction();
		
		try {
			session.delete(rol);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	/* (non-Javadoc)
	 * @see solicitudes.dao.DAORol#editarRol(solicitudes.entity.Rol)
	 */
	@Override
	public void editarRol(Rol rol) {
		
		Session session = getSession();
		session.beginTransaction();
		
		try {
			session.merge(rol);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

}
